package org.dromara.activity.rest;


import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import org.dromara.activity.domain.bo.ActivityUserBo;
import org.dromara.activity.domain.vo.ActivityUserVo;
import org.dromara.activity.service.ActivityUserService;
import org.dromara.common.core.domain.R;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 活动用户
 * @author Administrator
 * @date 2024/01/16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/activity/user")
public class ActivityUserRest extends BaseController {


    private final ActivityUserService activityUserService;


    /**
     * 获取用户信息列表
     */
    @SaCheckPermission("system:User:list")
    @GetMapping("/list")
    public TableDataInfo<ActivityUserVo> list(ActivityUserBo user, PageQuery pageQuery) {
        return activityUserService.selectPageUserList(user, pageQuery);
    }



    /**
     * 新增用户
     */
    @SaCheckPermission("system:User:add")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody ActivityUserBo user) {
//        if (!activityUserService.checkUserNameUnique(user)) {
//            return R.fail("新增用户'" + user.getUserName() + "'失败，用户名称已存在");
//        }
        return toAjax(activityUserService.insertUser(user));
    }

    /**
     * 修改用户
     */
    @SaCheckPermission("system:User:edit")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody ActivityUserBo user) {
//        if (!activityUserService.checkUserNameUnique(user)) {
//            return R.fail("修改用户'" + user.getUserName() + "'失败，用户名称已存在");
//        }
        return toAjax(activityUserService.updateUser(user));
    }


    /**
     * 状态修改
     */
    @SaCheckPermission("system:User:edit")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@RequestBody ActivityUserBo user) {
        return toAjax(activityUserService.updateUserStatus(user.getId(), user.getStatus()));
    }

    /**
     * 删除用户
     *
     * @param ids 用户ID串
     */
    @SaCheckPermission("system:User:remove")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        return toAjax(activityUserService.deleteUserByIds(ids));
    }
}
